import upload from '@dfeidao/atom-mobile/fs/upload';
import camera from '@dfeidao/atom-mobile/image-crop-picker/camera';
import { IFeidaoAiMobile } from '@dfeidao/atom-mobile/interfaces';
import get from '@dfeidao/atom-mobile/local/get';
import nodejs from '@dfeidao/atom-mobile/msg/nodejs';
import service from '@dfeidao/atom-mobile/msg/service';
import toast from '@dfeidao/atom-mobile/msgbox/toast';
import render from '@dfeidao/atom-mobile/render/render';
import { file_url, fileurl, productid, secretId, secretKey, spaceid, systemid } from '../atom/config';
import remove_file from '../atom/remove-file';

export default async function a005(fd: IFeidaoAiMobile, id: string) {
	// 签到
	let filename_id = ''; // 录入失败删除图片
	try {
		// 调用相机
		const img = await camera({
			width: 30,
			height: 40,
			cropping: false,
			compressImageQuality: 0.5
		}) as { path: string, mime: string };
		/* 返回数据 :height:400
		mime:"image/jpeg"
		modificationDate:"1555937426000"
		path:"file:///storage/emulated/0/Pictures/e64fc5cf-01a6-4610-9322-2fa75837346e.jpg"
		size:98370
		width:300
		*/
		const files = [
			{
				name: '',
				filename: img.mime.replace('image/', ''),
				filepath: img.path,
				filetype: img.mime
			}
		];
		const param = get(fd, 'params') as {
			teacher_name: string;
			productid: string;
			teacher_no: string;
		};
		render(fd, { display: 1 });
		const ress = await upload(fd, {
			files,
			toUrl: `${file_url}/upload?productid=${productid}`,
			method: 'POST',
			headers: {
				'Accept': 'application/json'
			}
		}, 'a007', 'a008') as { code: number, msg: [{ _id: string, filename: string }] };
		filename_id = ress.msg[0].filename;
		// chunkSize:261120
		// contentType:"image/jpg"
		// filename:"0e931acc-d9d2-47c6-814e-ed8141645f30.jpg"
		// length:5641528
		// md5:"33cbaba54e5d22c0dd196874c5eb3b59"
		// metadata:{originialFileName: "img.jpg", productid: "teach_sys"}
		// uploadDate:"2019-04-25T11:33:58.860Z"
		// _id:"c068a622-aea6-422a-a871-90557c9c0a68"
		console.log(ress);
		if (ress.code) {
			const q = {
				productid,
				spaceid,
				systemid,
				secretId,
				secretKey,
				maxfacenum: 1,
				minfacesize: 80,
				maxpersonnum: 10,
				fileurl,
				fileid: ress.msg[0].filename
			};
			const res1 = await service<{
				Results: Array<{ candidates: Array<{ faceId: string; personId: string; score: number; }> }>,
				msg: string,
				code: number
			}>('thirdpart.tencentcloud.searchpersonface', q);
			console.log(res1);

			if (res1.code === 1) {
				const candidates = res1.Results[0].candidates;
				if (candidates.length) {
					const score = candidates[0].score;
					if (score >= 95) {

						const params = {
							productid: param.productid,
							teacher_name: param.teacher_name,
							teacher_no: param.teacher_no,
							student_no: candidates[0].personId
						} as {
							teacher_name: string;
							productid: string;
							teacher_no: string;
							student_no: string;
							class_id: string;
							class_name: string;
							organization_type: string;
						};
						const class_res = get(fd, 'class_res') as [{ class_id: string, class_name: string, organization_type: string }];
						for (let i = 0; i < class_res.length; i++) {
							if (id === class_res[i].class_id) {
								params.class_id = class_res[i].class_id;
								params.class_name = class_res[i].class_name;
								params.organization_type = class_res[i].organization_type;
							}
						}
						const d = await nodejs<{ code: number, msg: string }>('app-home/s003', {
							params
						});
						console.log(d.msg);
						if (d.code) {
							toast({
								type: 'success',
								text: d.msg,
								position: 'top'
							});
						} else {
							toast({
								type: 'danger',
								text: d.msg,
								position: 'top'
							});
						}
					} else {
						console.log('该学生不是本班学生');
						toast({
							type: 'danger',
							text: '该学生不是本班学生',
							position: 'top'
						});
					}
				}
			} else {
				console.log('未检测人脸');
				toast({
					type: 'danger',
					text: '未检测人脸',
					position: 'top'
				});
			}


		} else {
			console.log('上传失败');
			toast({
				type: 'danger',
				text: '上传失败',
				position: 'top'
			});
		}
	} catch (err) {
		// 删除服务器图片
		if (filename_id) {
			await remove_file(file_url, productid, [filename_id]);
		}
		console.log(err);
		if (err.indexOf('图片中没有人脸') === -1) {
			toast({
				type: 'danger',
				text: '图片中没有人脸',
				position: 'top'
			});
		} else {
			toast({
				type: 'danger',
				text: '上传失败',
				position: 'top'
			});
		}
	} finally {
		render(fd, { display: 0, progress: 0 });
	}
}
